package com.ezhuan.common.pojo;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import com.alibaba.fastjson.JSONObject;
import com.ezhuan.common.util.ConstatFinalUtil;
import com.ezhuan.common.util.DateFormatUtil;
import com.jfinal.plugin.activerecord.Record;

/**
 * 所有POJO的父类
 * @author wangshSxt
 *
 */
public abstract class BasePojo<T>
{
	/* 主键 */
	protected int id ; 
	
	/* 返回表的名称 */
	public abstract String getTableName();
	
	/**
	 * 根据当前对象组装JSON
	 * @return
	 */
	public JSONObject toJSON()
	{
		DateFormatUtil dateFormatUtil = new DateFormatUtil();
		JSONObject regTempJSON = new JSONObject();
		/* 利用反射设置值 */
		try
		{
			Class class1 = this.getClass() ; 
			Field[] fields = class1.getDeclaredFields();
			for (int i = 0; i < fields.length; i++)
			{
				Field field = fields[i];
				/* Type类型 */
				String fieldName = field.getName() ; 
				String fieldNameStr = (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1, fieldName.length()); 
				Method method = class1.getMethod("get" + fieldNameStr, null);
				/* 取得属性对应的值 */
				Object obj = method.invoke(this, null);
				if(obj != null)
				{
					if(obj instanceof Date)
					{
						Date date = (Date) obj ; 
						/* 取值 */
						regTempJSON.put(fieldName, dateFormatUtil.formatDateTime(date));
					}else if(obj instanceof BasePojo)
					{
						BasePojo basePojo = (BasePojo) obj ; 
						/* 取值 */
						regTempJSON.put(fieldName, basePojo.toJSON());
					}else
					{
						regTempJSON.put(fieldName, obj + "");
					}
				}
			}
			regTempJSON.put("id", this.getId() + "");
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("封装JSON对象出错了",e);
		}
		return regTempJSON ;
	}
	
	/**
	 * 将当前记录转换成Record
	 * 以Str和Object结尾的属性会被过滤掉
	 * @return
	 */
	public Record toRecord()
	{
		Record record = new Record();
		/* 利用反射设置值 */
		try
		{
			Class class1 = this.getClass() ; 
			Field[] fields = class1.getDeclaredFields();
			for (int i = 0; i < fields.length; i++)
			{
				Field field = fields[i];
				/* Type类型 */
				String fieldName = field.getName() ; 
				String fieldNameStr = (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1, fieldName.length()); 
				Method method = class1.getMethod("get" + fieldNameStr, null);
				/* 取得属性对应的值 */
				Object obj = method.invoke(this, null);
				
				/* 以Str结尾的过滤掉 */
				if(fieldName.endsWith("Str") || fieldName.endsWith("Object"))
				{
					continue ; 
				}
				
				record.set(fieldName, obj);
			}
			record.set("id", this.getId());
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("封装Record对象出错了",e);
		}
		return record;
	}
	
	/**
	 * 将Record转换成BasePojo
	 * 以Str和Object结尾的属性会被过滤掉
	 * 
	 * @param record
	 * @return
	 */
	public T parseRecord(Record record)
	{
		/* 利用反射设置值 */
		Class class1 = this.getClass() ; 
		Field[] fields = class1.getDeclaredFields();
		for (int i = 0; i < fields.length; i++)
		{
			Field field = fields[i];
			try
			{
				/* Type类型 */
				Class typeClass = field.getType();
				/* 类型的字符串描述*/
				String typeStr = field.getType().getSimpleName() ; 
				String fieldName = field.getName() ; 
				/* 以Str结尾的过滤掉 */
				if(fieldName.endsWith("Str") || fieldName.endsWith("Object"))
				{
					continue ; 
				}
				
				String fieldNameStr = (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1, fieldName.length()); 
				Method method = class1.getMethod("set" + fieldNameStr, typeClass);
				
				/* 取得属性对应的值 */
				if("int".equalsIgnoreCase(typeStr))
				{
					Object obj = method.invoke(this, record.getInt(fieldName));
				}else if("double".equalsIgnoreCase(typeStr))
				{
					Object obj = method.invoke(this, Double.valueOf(record.get(fieldName) + ""));
				}else if("byte".equalsIgnoreCase(typeStr))
				{
					Object obj = method.invoke(this, Byte.valueOf(record.get(fieldName) + ""));
				}else if("Date".equalsIgnoreCase(typeStr))
				{
					Object obj = method.invoke(this, record.getTimestamp(fieldName));
				}else
				{
					if(record.get(fieldName) != null)
					{
						Object obj = method.invoke(this, record.get(fieldName) + "");
					}
				}
			} catch (Exception e)
			{
				//ConstatFinalUtil.LOGGER.error("封装Record对象出错了",e);
			}
		}
		this.setId(record.getInt("id"));
		return (T) this;
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}
}
